package com.example.wechat.csdnandroid.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wechat.csdnandroid.R;
import com.example.wechat.csdnandroid.activity.RecyclerViewActivity;
import com.example.wechat.csdnandroid.bean.Fruit;

import java.util.ArrayList;

public class FruitRecyclerViewAdapter extends RecyclerView.Adapter<FruitRecyclerViewAdapter.RVViewHolder> {
    private ArrayList<Fruit> fruits;
    private Activity activity;
    private static final String TAG = "FruitRecyclerViewAdapte";
    public FruitRecyclerViewAdapter(ArrayList<Fruit> data, RecyclerViewActivity recyclerViewActivity) {
        this.fruits = data;
        this.activity = recyclerViewActivity;
    }

    @NonNull
    @Override
    public RVViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_pic_text, parent, false);
        RVViewHolder holder = new RVViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final RVViewHolder holder, final int position) {
        final Fruit fruit = fruits.get(position);
        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int adapterPosition = holder.getAdapterPosition();
                Log.e(TAG, "onClick:holder.iv "+adapterPosition );
            }
        });
        holder.iv.setImageResource(fruit.getPicImageRes());
        holder.tv.setText(fruit.getPicText());

    }

    @Override
    public int getItemCount() {
        return fruits == null ? 0 : fruits.size();
    }

    static class RVViewHolder extends RecyclerView.ViewHolder {

        ImageView iv;
        TextView tv;

        public RVViewHolder(@NonNull View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = getAdapterPosition();
                    Log.e(TAG, "onClick:itemView "+adapterPosition );

                }
            });
            iv = itemView.findViewById(R.id.iv_ilpt);
            tv = itemView.findViewById(R.id.tv_ilpt);
        }


    }
}
